package com.dayatang.android.map;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class PersonLocationOverItem extends ItemizedOverlay<OverlayItem> {

	private Drawable marker;
	
	private List<OverlayItem> geoList = new ArrayList<OverlayItem>();
	
	public PersonLocationOverItem(Drawable defaultMarker,Context context,List<GeoPoint> geoPoints) {
		super(boundCenterBottom(defaultMarker));
		
		this.marker = defaultMarker;
		
		for(GeoPoint geoPoint : geoPoints){
			geoList.add(new OverlayItem(geoPoint, "", "test"));
		}
		//该方法应该是将当前的GeoList的overlay 同步到 父类ItemizedOverlay中的list中以进行重绘  
		//所以当GeoList数据发生变化时需要调用该方法
		populate();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		
		Projection projection = mapView.getProjection();
		
		for (int index = size() - 1; index >= 0; index--) {
			
			OverlayItem overLayItem = getItem(index);
			String title = overLayItem.getTitle();
			
			// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
			Point point = projection.toPixels(overLayItem.getPoint(), null); 
			
			Paint paintCircle = new Paint();
			paintCircle.setColor(Color.RED);
			canvas.drawCircle(point.x, point.y, 5, paintCircle); // 画圆

			Paint paintText = new Paint();
			paintText.setColor(Color.BLACK);
			paintText.setTextSize(15);
			canvas.drawText(title, point.x, point.y - 25, paintText); // 绘制文本
		}
		
		super.draw(canvas, mapView, shadow);
		//调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
		boundCenterBottom(marker);
		
	}
	
	protected boolean onTap(int index) {
		return true;
	}
	
	protected OverlayItem createItem(int i) {
		return geoList.get(i);
	}

	public int size() {
		return geoList.size();
	}

}
